<template>
	<!-- 确认订单 -->
	<view class="container">
		<!-- 标题 -->
		<headTitle title="确认订单" :bottomline="true"></headTitle>
		<navigator url="/pages/address/address?source=1" class="address-section bgfff" v-if="allData">
			<view class="order-content flex-center">
				<view class="cen flex-column" v-if="addressData">
					<view class="flex-center">
						<view class="default" v-if="addressData.defaultStatus == 1">默认</view>
						<text class="font24 color333">{{ addressData.province }} {{ addressData.city }} {{ addressData.region }}</text>
					</view>
					<text class="middle font36 color333 fontb">
						{{ addressData.detailAddress }}
					</text>
					<text class="font26 color666">
						{{ addressData.name }} {{ addressData.phoneNumber | cipherTepl }}
					</text>
				</view>
				<view class="cen flex-center font36 color333 fontb" v-else>
					<text>请设置收货地址</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>

			<image class="a-bg" src="/static/image/submitline.png"></image>
		</navigator>
		
		<view class="content" v-if="allData">
			<view class="shops bgfff" v-for="(item, index) in list" :key='index'>
				<view class="shop flex-center" v-for="(val, idx) in item.goods" :key="idx">
					<image :src="val.productImage" mode="aspectFill"></image>
					<view class="info flex-column">
						<text class="title font28 color333">{{ val.productName }}</text>
						<text class="attr font26 color999">{{ val.sp }}</text>
						<view class="money flex-center-between">
							<view class="color333">
								<text class="font24">¥</text><text class="font30">{{ val.productPrice }}</text>
							</view>
							<text class="font26 color333">x{{ val.productNum }}</text>
						</view>
					</view>
				</view>
				<view class="s-top">
					<view class="s-left">
						<text class="font28 color666">运费</text>
					</view>
					<view class="s-right txt-r">
						<text class="font28 color333">¥{{item.freightAmount}}</text>
					</view>
				</view>
				<view class="s-top">
					<view class="s-left">
						<text class="font28 color666">优惠券</text>
					</view>
					<view class="s-right txt-r" @tap="navToTabPage('/pages/coupon/cartCoupon?index=' + index)">
						<text class="font28 red" v-if="item.promotionAmount">-¥{{item.promotionAmount}}</text>
						<text class="yticon icon-you"></text>
					</view>
				</view>
				<view class="s-top not">
					<view class="s-left">
						<text class="font28 color666">买家留言</text>
					</view>
					<view class="s-right">
						<textarea v-model="item.message" class="font28" placeholder="给商家留言，最多50个字"  auto-height maxlength="50" @focus="show = false" @blur='show = true' />
					</view>
				</view>
			</view>
			<view class="count flex-column">
				<view class="flex-center-between">
					<text class="color666">商品总额</text><text class="color333">¥{{ shopCount }}</text>
				</view>
				<view class="middle flex-center-between">
					<text class="color666">运费</text><text class="color333">¥{{ freight }}</text>
				</view>
				<view class="flex-center-between">
					<text class="color666">优惠</text><text class="color333">-¥{{ coupon }}</text>
				</view>
			</view>
		</view>

		<!-- 底部 -->
		<view class="footer flex-center bgfff" v-show="show" v-if="allData">
			<view class="flex-center-center">
				<view>
					<text class="font24 color999">共1件,</text>
					<text class="font28 color333">合计:</text>
					<text class="font24 red">¥</text>
					<text class="font32 fontb red">{{ payMoney }}</text>
				</view>
			</view>
			<button class="submit flex-center-center font28 colorfff" @click="submit">提交订单</button>
		</view>
	</view>
</template>

<script>
import Api from '@/common/api'
import headTitle from '@/components/head-title.vue'
export default {
	data() {
		return {
			addressData: null, // 地址数据
			height: 0, // 屏幕高度
			show: true, // 键盘弹起隐藏按钮
			allData: null, // 确认订单的数据
			list: [], // 订单列表
			shopCount: 0, // 商品总额
			freight: 0, // 运费金额
			coupon: 0, // 优惠金额
			payMoney: 0, // 应付金额
			money: 0, // 支付页面获取数据
		}
	},
	async onLoad(option) {
		this.handleData()
		// 获取窗口可视高度
		this.height = uni.getSystemInfoSync().windowHeight
		// 处理键盘弹出和收起
		uni.onWindowResize((res) => {
			if(this.height === res.size.windowHeight) {
				this.show = true
			}
		})
	},
	components: {
		headTitle
	},
	methods: {
		// 选择优惠券后计算价格
		calculatePrice() {
			this.coupon = 0
			this.list.map(item => {
				this.coupon += +item.promotionAmount
			})
			this.payMoney = this.shopCount + this.freight - this.coupon
		},
		// 处理数据
		handleData() {
			// 获取上一个页面提交的数据
			this.allData = this.$api.prePage().submitData
			if(this.allData) {
				this.addressData = this.allData.address
				this.list = this.allData.storeConfirmGoodInfoList
				this.shopCount = this.allData.totalAmount || 0 // 商品总额
				this.freight = this.allData.freightAmount || 0 // 运费金额
				this.coupon = this.allData.promotionAmount || 0 // 优惠金额
				this.payMoney = this.allData.payAmount || 0 // 应付金额
			}
		},
		// 页面跳转
		navToTabPage(url) {
			uni.navigateTo({ url })
		},
		async submit() {
			if (!(this.addressData && this.addressData.id)) {
				this.$api.msg('请选择收货地址')
				return
			}
			let params = this.$api.prePage().params
			params.addressId = this.addressData.id
			let coupon = []
			let content = []
			this.list.map(item => {
				coupon.push({
					storeId: item.storeId,
					memberCouponId: item.couponHistoryId,
				})
				content.push({
					storeId: item.storeId,
					buyContent: item.message,
				})
			})
			params.storeMemberCouponId = JSON.stringify(coupon)
			params.storeContent = JSON.stringify(content)
			let data = await Api.apiCall('post', Api.order.submitOrder, params, true)
			if(data) {
				this.money = data.payAmount
				this.navToTabPage('/pages/money/money?orderId=' + data.totalOrderSn)
			}
		},
		stopPrevent() {}
	}
};
</script>

<style lang="scss">
page {
	background-color: #EEEEEE;
	padding-bottom: 110rpx;
}

.address-section {
	padding: 40rpx 30rpx;
	margin-bottom: 20rpx;
	position: relative;
	min-height: 177rpx;
	.order-content {
		min-height: 97rpx;
		.cen {
			width: 645rpx;
			.default {
				width: 67rpx;
				height: 32rpx;
				text-align: center;
				border-radius: 21rpx;
				font-size: 20rpx;
				background-color: #D7BD9F;
				color: #85623B;
				margin-right: 12rpx;
			}
			.middle {
				margin: 12rpx 0;
				letter-spacing: 1.32rpx;
			}
		}
		.icon-you {
			font-size: 22rpx;
			color: #333;
			margin-left: 30rpx;
		}
	}
	.a-bg {
		position: absolute;
		left: 0;
		bottom: 0;
		display: block;
		width: 100%;
		height: 6rpx;
	}
}

.content {
	&>view {
		margin-bottom: 20rpx;
		background-color: #fff;
	}
	.shops {
		padding: 30rpx 30rpx 40rpx;
		.shop{
			&:not(:last-child) {
				margin-bottom: 30rpx;
			}
			image {
				width: 160rpx;
				height: 160rpx;
				border-radius: 5rpx;
				margin-right: 24rpx;
			}
			.info {
				width: calc(100% - 184rpx);
				.title {
					overflow: hidden;
					display: -webkit-box;
					text-overflow: ellipsis;
					-webkit-line-clamp: 1;  /*要显示的行数*/
					-webkit-box-orient: vertical;
				}
				.attr {
					margin: 12rpx 0 16rpx;
				}
				.money {
					line-height: 42rpx;
					align-items: flex-end;
				}
			}
		}
		.s-top {
			display: flex;
			align-items: flex-end;
			margin-top: 40rpx;
			line-height: 40rpx;
			&.not {
				align-items: initial;
			}
			.s-left {
				width: 160rpx;
				text-align: right;
				margin-right: 24rpx;
			}
			.s-right {
				width: calc(100% - 184rpx);
				.icon-you {
					font-size: 22rpx;
					color: #999;
					margin-left: 20rpx;
				}
				textarea {
					padding-top: 4rpx;
					width: 100%;
					line-height: 40rpx;
					&/deep/.uni-textarea-placeholder {
						color: #ccc;
					}
				}
				&.txt-r {
					display: flex;
					justify-content: flex-end;
				}
			}
		}
	}
	.count {
		padding: 30rpx;
		font-size: 28rpx;
		.middle {
			margin: 30rpx 0;
		}
	}
}

.red {
	color: #F0421E;
}

.footer {
	border-top: 1rpx solid #ddd;
	position: fixed;
	left: 0;
	bottom: 0;
	z-index: 99;
	width: 100%;
	height: 110rpx;
	justify-content: flex-end;
	.submit {
		margin: 0 30rpx;
		width: 208rpx;
		height: 80rpx;
		background-color: #C84739;
		border-radius: 40rpx;
	}
}
</style>
